<?php
/* This file is part of Mirasol CMS
   (C) 2011 by Chris Alban Hansen.
   Released under the terms of the GNU General Public License.
   See COPYING in the top level directory of the Mirasol CMS installation. */

include "{$_SERVER['DOCUMENT_ROOT']}/includes/config.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/xmlfeed.php";

if ($loadsubpage == 1)
{
  $sid = isset ($_GET['sid']) ? $_GET['sid'] : 0;
  
  /* Get site name */
  $result = mysql_query ("SELECT sitename, domainname, protocol FROM ".db_maketablename ($table_settings)." LIMIT 1");
  if (mysql_num_rows ($result) > 0)
    {
      $row = mysql_fetch_array ($result);
      $sitename = htmlentities ($row['sitename'], ENT_COMPAT, "UTF-8", false);
      $domainname = htmlentities ($row['domainname'], ENT_COMPAT, "UTF-8", false);
      $protocol = $row['protocol'] != "" ? $row['protocol'] : "http";
    }
  mysql_free_result ($result);
?>
<h1>
  <?=$sitename?>
  <a href="<?=$protocol?>://<?=$domainname?>/" title="Go to website" rel="external"><img src="./images/link.gif" alt="CodeMirror" class="externallink" /></a>
</h1>

<div id="templates">
  <h3>Styles</h3>
  <table cellpadding="0" cellspacing="0" style="width: 100%">
<?php
/* Print the list of styles */
$result = mysql_query ("SELECT id, name FROM ".db_maketablename ($table_styles)." ORDER BY name");
if (mysql_num_rows ($result) > 0)
  {
    while ($row = mysql_fetch_array ($result))
      {
        $name = htmlentities ($row['name'], ENT_COMPAT, "UTF-8", false).".css";
        print "<tr><td><a href=\"./?p=styles&amp;sid={$row['id']}\"><strong>$name</strong></a></td>";
        print "<td class=\"alignright\"><a href=\"javascript:delstyle('$name', {$row['id']});\" title=\"Delete '$name'\"><strong>&times;</strong></a></td></tr>\n";
      }
  }
mysql_free_result ($result);
?>
  </table>

  <div id="addnew1">
    <p>
      <input type="button" value="+" onclick="document.getElementById('addnew2').style.display='block';" class="button" />
      Add new style
    </p>
  </div>

  <div id="addnew2">
    <form action="../includes/addstyle.php" method="post">
      <table cellpadding="0" cellspacing="0">
        <tr>
          <td><strong>Name:</strong>&nbsp;</td>
          <td><input type="text" name="name" value="Type_name_here" maxlength="50" class="textfield" />.css</td>
        </tr>
        <tr>
          <td colspan="2"><input type="submit" value="Add &raquo;" class="button" /></td>
        </tr>
      </table>
    </form>
  </div>
</div>

<div id="editor">
<?php
/* If no style is selected... */
if ($sid == 0)
{
?>
  <div id="info">
    <p>&larr; Click on an item in the list to edit it.</p>
  </div>
<?php
}
else
{
?>
    <div id="tabs">
      <ul>
        <li><span>Style</span></li>
      </ul>
    </div>
    
    <form action="../includes/savestyle.php" method="post">
      <div id="tab1">
<?php
  $result = mysql_query ("SELECT name, css FROM ".db_maketablename ($table_styles)." WHERE id='$sid'");
  if (mysql_num_rows ($result) > 0)
    {
      $row = mysql_fetch_array ($result);
      
      $style = htmlentities ($row['css'], ENT_COMPAT, "UTF-8", false);
      
      print "<label>Name</label><br /><input type=\"text\" name=\"stylename\" value=\"".htmlentities ($row['name'], ENT_COMPAT, "UTF-8", false)."\" class=\"textfield\" />\n";
?>
      <label>Style</label>
      <textarea id="css" name="style"><?=$style?></textarea>
      <script type="text/javascript">
        //<![CDATA[
          var editor = CodeMirror.fromTextArea (document.getElementById ("css"), { mode: { name: "css" } });
        //]]>
      </script>
<?php
    }
  mysql_free_result ($result);
?>
      </div>
      <p class="alignright operate">&nbsp;<br /><input type="hidden" name="sid" value="<?=$_GET['sid']?>" /><input type="submit" value="Save" class="button" /></p>
    </form>
<?php
}
?>
</div>

<div class="clear"></div>
<?php
}
?>
